package com.berry_med.handsetmonitor.volleyFramework;

import android.content.Context;
import android.os.Handler;

import com.andware.volley.Request;
import com.andware.volley.Response;
import com.andware.volley.Response.ErrorListener;
import com.andware.volley.VolleyError;
import com.andware.volley.toolbox.GsonRequest;
import com.andware.volley.toolbox.SubStringRequest;
import com.berry_med.handsetmonitor.MyEvent.MyVolleyEvent;


/**
 * Created by ishishuji on 14-7-10.
 */
public class MyVolley extends MyVolleyBase {


    public MyVolley ( Context context ) {
        super ( context );
    }


    public static < T > Request volleyStringBase ( String url, T request ) {

        SubStringRequest stringRequest;

        Response.Listener< String > baseListener = new Response.Listener< String > () {

            @Override
            public void onResponse ( String response ) {
                baseDeal ( response );
            }
        };


        stringRequest = new SubStringRequest ( getMethod (), url, request, baseListener, errorListener );

        addParams ( stringRequest );

        getRequestQueue ().add ( stringRequest );

        return stringRequest;
    }

    public static < T > Request volleyStringBase ( String url, T request , final Handler handler ) {

        SubStringRequest stringRequest;

        Response.Listener< String > baseListener1 = new Response.Listener< String > () {

            @Override
            public void onResponse ( String response ) {
//                baseDeal ( response );
                handler.obtainMessage ( StatusCode.SUCCESS,response ).sendToTarget ();
            }
        };

        ErrorListener errorListener1 = new ErrorListener () {
            @Override
            public void onErrorResponse ( VolleyError error ) {
                handler.obtainMessage ( StatusCode.FAILD , error ).sendToTarget ();
            }
        };

        stringRequest = new SubStringRequest ( getMethod (), url, request, baseListener1, errorListener1 );

        addParams ( stringRequest );

        getRequestQueue ().add ( stringRequest );

        return stringRequest;
    }

    public static < T > Request volleyStringBase ( String url ) {

        SubStringRequest stringRequest;


        Response.Listener< String > baseListener = new Response.Listener< String > () {

            @Override
            public void onResponse ( String response ) {
                baseDeal ( response );
            }
        };

        stringRequest = new SubStringRequest ( getMethod (), url, baseListener, errorListener );

        addParams ( stringRequest );

        getRequestQueue ().add ( stringRequest );

        return stringRequest;
    }

    public static < T > Request volleyStringBase ( String url, T request, final int position ) {

        SubStringRequest stringRequest;

        set_position ( position );

        Response.Listener< String > positionListener = new Response.Listener< String > () {

            @Override
            public void onResponse ( String response ) {
                positionDeal ( response, position );
            }
        };

        ErrorListener positionErrorListener = new ErrorListener () {

            @Override
            public void onErrorResponse ( VolleyError error ) {

                MyVolleyEvent< VolleyError > volleyEvent = new MyVolleyEvent< VolleyError > ();
                volleyEvent.set_error ( error );
                volleyEvent.setPosition ( position );
                volleyEvent.setStatus ( StatusCode.FAILD );

            }
        };

        stringRequest = new SubStringRequest ( getMethod (), url, request, positionListener, positionErrorListener );

        addParams ( stringRequest );

        getRequestQueue ().add ( stringRequest );

        return stringRequest;
    }


    public static < T > Request volleyStringBase ( String url, final int position ) {

        SubStringRequest stringRequest;

        set_position ( position );

        Response.Listener< String > positionListener = new Response.Listener< String > () {

            @Override
            public void onResponse ( String response ) {
                positionDeal ( response, position );
            }
        };


        ErrorListener positionErrorListener = new ErrorListener () {

            @Override
            public void onErrorResponse ( VolleyError error ) {

                MyVolleyEvent< VolleyError > volleyEvent = new MyVolleyEvent< VolleyError > ();
                volleyEvent.set_error ( error );
                volleyEvent.setPosition ( position );
                volleyEvent.setStatus ( StatusCode.FAILD );

            }
        };

        stringRequest = new SubStringRequest ( getMethod (), url, positionListener, positionErrorListener );

        addParams ( stringRequest );

        getRequestQueue ().add ( stringRequest );

        return stringRequest;
    }

    public static < T > Request volleyStringBase ( String url, final int position , final Handler handler ) {

        SubStringRequest stringRequest;

        set_position ( position );

        Response.Listener< String > positionListener = new Response.Listener< String > () {

            @Override
            public void onResponse ( String response ) {
                positionDeal ( response, position );
                handler.obtainMessage ( StatusCode.SUCCESS , position , 0 , response ).sendToTarget ();
            }
        };


        ErrorListener positionErrorListener = new ErrorListener () {

            @Override
            public void onErrorResponse ( VolleyError error ) {
                handler.obtainMessage ( StatusCode.FAILD , position , 0 , error ).sendToTarget ();

            }
        };

        stringRequest = new SubStringRequest ( getMethod (), url, positionListener, positionErrorListener );

        addParams ( stringRequest );

        getRequestQueue ().add ( stringRequest );

        return stringRequest;
    }

    public static < T > Request volleyGsonBase ( String url, T request, Class< T > cls ) {

        Response.Listener< T > baseListener = new Response.Listener< T > () {

            @Override
            public void onResponse ( T response ) {
                baseDeal ( response );
            }
        };

        GsonRequest gsonRequest = new GsonRequest ( getMethod (), url, request, cls, baseListener, errorListener );

        addParams ( gsonRequest );

        getRequestQueue ().add ( gsonRequest );

        return gsonRequest;
    }

    public static < T, U > Request volleyGson ( String url, T request, Class< U > cls ) {

        Response.Listener< U > baseListener = new Response.Listener< U > () {

            @Override
            public void onResponse ( U response ) {

                baseDeal ( response );

            }
        };

        GsonRequest gsonRequest = new GsonRequest ( getMethod (), url, request, cls, baseListener, errorListener );

        addParams ( gsonRequest );

        getRequestQueue ().add ( gsonRequest );

        return gsonRequest;
    }

    public static < T, U > Request volleyGson ( String url, final T request, Class< U > cls, final Handler handler ) {

        Response.Listener< U > baseListener1 = new Response.Listener< U > () {

            @Override
            public void onResponse ( U response ) {

                handler.obtainMessage ( StatusCode.SUCCESS , response ).sendToTarget ();

            }
        };
        ErrorListener errorListener1 = new ErrorListener () {

            @Override
            public void onErrorResponse ( VolleyError error ) {
                handler.obtainMessage ( StatusCode.FAILD , error ).sendToTarget ();
            }
        };


        GsonRequest gsonRequest = new GsonRequest ( getMethod (), url, request, cls, baseListener1, errorListener1 );

        addParams ( gsonRequest );

        getRequestQueue ().add ( gsonRequest );

        return gsonRequest;
    }

    public static < T,U > Request volleyGson ( String url, T request ) {

        Response.Listener< U > baseListener = new Response.Listener< U > () {

            @Override
            public void onResponse ( U response ) {

                baseDeal ( response );

            }
        };

        GsonRequest gsonRequest = new GsonRequest ( getMethod (), url, request, null, baseListener, errorListener );

        addParams ( gsonRequest );

        getRequestQueue ().add ( gsonRequest );

        return gsonRequest;
    }

    public static < T > Request volleyGsonBase ( String url, Class< T > cls ) {

        Response.Listener< T > baseListener = new Response.Listener< T > () {

            @Override
            public void onResponse ( T response ) {
                baseDeal ( response );
            }
        };

        GsonRequest gsonRequest = new GsonRequest ( getMethod (), url, cls, baseListener, errorListener );

        addParams ( gsonRequest );

        getRequestQueue ().add ( gsonRequest );

        return gsonRequest;
    }

    public static < T > Request volleyGsonBase ( String url, T request, Class< T > cls, final int position ) {

        set_position ( position );

        Response.Listener< T > positionListener = new Response.Listener< T > () {

            @Override
            public void onResponse ( T response ) {
                positionDeal ( response, position );
            }
        };

        ErrorListener positionErrorListener = new ErrorListener () {

            @Override
            public void onErrorResponse ( VolleyError error ) {

                MyVolleyEvent< VolleyError > volleyEvent = new MyVolleyEvent< VolleyError > ();
                volleyEvent.set_error ( error );
                volleyEvent.setPosition ( position );
                volleyEvent.setStatus ( StatusCode.FAILD );

            }
        };

        GsonRequest gsonRequest = new GsonRequest ( getMethod (), url, request, cls, positionListener, positionErrorListener );

        addParams ( gsonRequest );

        getRequestQueue ().add ( gsonRequest );

        return gsonRequest;
    }

    public static < T, U > Request volleyGson ( String url, T request, Class< U > cls, final int position ) {

        set_position ( position );

        Response.Listener< U > positionListener = new Response.Listener< U > () {

            @Override
            public void onResponse ( U response ) {
                positionDeal ( response, position );
            }
        };


        ErrorListener positionErrorListener = new ErrorListener () {

            @Override
            public void onErrorResponse ( VolleyError error ) {

                MyVolleyEvent< VolleyError > volleyEvent = new MyVolleyEvent< VolleyError > ();
                volleyEvent.set_error ( error );
                volleyEvent.setPosition ( position );
                volleyEvent.setStatus ( StatusCode.FAILD );

            }
        };

        GsonRequest gsonRequest = new GsonRequest ( getMethod (), url, request, cls, positionListener, positionErrorListener );

        addParams ( gsonRequest );

        getRequestQueue ().add ( gsonRequest );

        return gsonRequest;
    }

    public static < T, U > Request volleyGson ( String url, T request, Class< U > cls, final int position , final Handler handler ) {

        set_position ( position );

        Response.Listener< U > positionListener = new Response.Listener< U > () {

            @Override
            public void onResponse ( U response ) {

                handler.obtainMessage ( StatusCode.SUCCESS ,position , 0, response ).sendToTarget ();

            }
        };
        ErrorListener positionErrorListener = new ErrorListener () {

            @Override
            public void onErrorResponse ( VolleyError error ) {
                handler.obtainMessage ( StatusCode.FAILD ,position ,0,  error ).sendToTarget ();
            }
        };

        GsonRequest gsonRequest = new GsonRequest ( getMethod (), url, request, cls, positionListener, positionErrorListener );

        addParams ( gsonRequest );

        getRequestQueue ().add ( gsonRequest );

        return gsonRequest;
    }

    public static < T > Request volleyGsonBase ( String url, Class< T > cls, final int position ) {

        set_position ( position );

        Response.Listener< T > positionListener = new Response.Listener< T > () {

            @Override
            public void onResponse ( T response ) {
                positionDeal ( response, position );
            }
        };


        ErrorListener positionErrorListener = new ErrorListener () {

            @Override
            public void onErrorResponse ( VolleyError error ) {

                MyVolleyEvent< VolleyError > volleyEvent = new MyVolleyEvent< VolleyError > ();
                volleyEvent.set_error ( error );
                volleyEvent.setPosition ( position );
                volleyEvent.setStatus ( StatusCode.FAILD );

            }
        };

        GsonRequest gsonRequest = new GsonRequest ( getMethod (), url, cls, positionListener, positionErrorListener );

        addParams ( gsonRequest );

        getRequestQueue ().add ( gsonRequest );

        return gsonRequest;
    }

}
